package com.lebang.controller.picture;



import com.lebang.service.ImageSaveService;
import com.lebangweb.response.Callback;
import com.lebangweb.response.JsonData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;


import java.io.IOException;
import java.io.UnsupportedEncodingException;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @Author: hwq
 * @Date: 2019/5/21 10:00
 * @Version 1.0
 */


@RestController
@RequestMapping("/picture/qiniu")
public class QiNiu {

    private static final String PREF_NAME = "task/";
    private static final String HOST_NANE = "http://imgxy.lbappstore.com";


    @Autowired
    private ImageSaveService imageSaveService;

    /**
     * 上传用户图片
     *
     * @param
     * @return
     * @author hwq
     * @date 2019/5/21
     */

    @RequestMapping(value = {"/upload"}, method = {RequestMethod.POST})
    @ResponseBody
    public JsonData upload(MultipartFile file, String type) throws IOException {
        String filename = "";
        if (type != null) {
            if (file.getOriginalFilename().indexOf(".") > 0) {
                filename = file.getOriginalFilename().substring(Objects.requireNonNull(file.getOriginalFilename()).indexOf("."));
            } else {
                filename = file.getOriginalFilename() + type;
            }
        } else {
            if (file.getOriginalFilename().indexOf(".") > 0) {
                filename = file.getOriginalFilename().substring(Objects.requireNonNull(file.getOriginalFilename()).indexOf("."));
            } else {
                filename = file.getOriginalFilename();
            }
        }
        try {

            String objName =imageSaveService.saveTaskImg(filename, file.getInputStream());


            if (objName != null){
                return Callback.success("success", "上传成功", HOST_NANE + "/" + objName);
            }
            else{
                return Callback.fail("error", "图片上传异常", "");
            }

        } catch (UnsupportedEncodingException ex) {
            //ignore
            return Callback.fail("success", "上传失败", null);
        } catch (IllegalStateException i) {
            return Callback.fail("success", "上传失败文件过大", null);
        }


    }

    /**
     * 批量上传用户图片
     *
     * @return
     * @author hwq
     * @date 2019/5/21
     */

    @RequestMapping(value = {"/uploadmore"}, method = {RequestMethod.POST})
    @ResponseBody
    public JsonData uploadmore(MultipartFile[] file, String type) throws IOException {
        List<String> stringList = new ArrayList<>();
        List<JsonData> list = new ArrayList<>();

        for (MultipartFile multipartFile : file) {
            if (multipartFile != null) {
                try {
                    JsonData jsonData = this.upload(multipartFile, null);
                    list.add(jsonData);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        list.forEach(s -> {
            if (s.isSuccess()) {
                stringList.add(s.getData().toString());
            }
        });
        if (stringList.size() > 0) {
            return Callback.success("success", "上传成功", stringList);
        } else {
            return Callback.fail("success", "上传失败", null);
        }

    }

    /**
     * 上传用户图片
     *
     * @param
     * @return
     * @author hwq
     * @date 2019/5/21
     */

    @RequestMapping(value = {"/upload/rank"}, method = {RequestMethod.POST})
    @ResponseBody
    public JsonData uploadrank(MultipartFile file, String type) throws IOException {
        String filename = "";
        if (type != null) {
            if (file.getOriginalFilename().indexOf(".") > 0) {
                filename = file.getOriginalFilename().substring(Objects.requireNonNull(file.getOriginalFilename()).indexOf("."));
            } else {
                filename = file.getOriginalFilename() + type;
            }
        } else {
            if (file.getOriginalFilename().indexOf(".") > 0) {
                filename = file.getOriginalFilename().substring(Objects.requireNonNull(file.getOriginalFilename()).indexOf("."));
            } else {
                filename = file.getOriginalFilename();
            }
        }
        try {

            String objName =imageSaveService.saveRankImg(filename, file.getInputStream());


            if (objName != null){
                return Callback.success("success", "上传成功", HOST_NANE + "/" + objName);
            }
            else{
                return Callback.fail("error", "图片上传异常", "");
            }

        } catch (UnsupportedEncodingException ex) {
            //ignore
            return Callback.fail("success", "上传失败", null);
        } catch (IllegalStateException i) {
            return Callback.fail("success", "上传失败文件过大", null);
        }


    }
}
